/* 
 * File:   naryTree.h
 * Author: PprrATeekK
 *
 * Created on October 22, 2012, 4:33 PM
 */

#include "vfs.h"

#pragma once
#ifndef nary_H
#define nary_H


// Structure definitions//


// Structure used to represent one node in nary tree //
typedef struct node {
    char *name;                 // name of Directory or File
    struct node* rightSib;      // pointer to right sibling of this node
    struct node* leftSib;       // pointer to left sibling of this node
    struct node* parent;        // pointer to parent node of this node
    struct node* child;         // pointer to first child of this node
    long fileDescOffset;        // Offset(in VFS) of file descriptor for this node
    char type;
    } directory;


directory* head;


//This structure(intermediate-node) is used to pack pointer(to parent) and 
//Directory name(to be added to tree), in recursive addition of directories//
typedef struct iNode {
    char* dirName;
    struct node* parent;
}iNode;

 


/*/////////////////////////////////////////////////////////////////////////////
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~//Function Prototypes//~~~~~~~~~~~~~~~~~~~~~~~~~~~
////////////////////////////////////////////////////////////////////////////*/


//Function to traverse directory structure (sibling first)//
void traverse(directory *head);


// Function to add a node as child(FileDescriptor* flDec) of given node(directory* parent)//
int addNode(directory* parent, directory* newDir);


//Finds the directory path(address) from root to given directory node, traversing backward//
char* findPath(directory* node);


//check if the given path exists and returns the pointer to parent of last available node//
directory* tracePath(directory *head, char* path);


//check if the given path exists and returns the structure iNode, containing //
//pointer to last available node as parent, and name of found node.          //
directory * traceIPath(char* path, iNode* inode);

// Function to create node in naryTree and initialize it with given values //
directory* creatNode(char *path, char* Name, char fileType, int recursive /* = 0:true; 1:false*/);


// Function to move node from source path to new destination path
int moveNode(char* sourcePath, char* destPath,char srcType);

int deleteNode(directory *curDir);

int initializeNAry(int i);

int mountNAry(FileDescriptor *fDes,long offset);

// Function to get the file or directory name from path: mode=0 for name, mode=1 for path
char* getFileDirName(char* path);

int updateAddress(FILE* updtPtr, directory* curDir);

int checkHead();

int setHead(directory* temp);

#endif
